﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace QuanLyKhachSanDAO
{
    public class DataProvider
    {
        private SqlCommand cmd = new SqlCommand();
        private SqlConnection cnn = new SqlConnection();
        public string strConnect = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\QuanLyKhachSan.mdf;Integrated Security=True;User Instance=True";
        //private string strConnect = @"Data Source=TanLegend-PC;Initial Catalog=Phong;Integrated Security=True";
        public DataProvider()
        {
            cnn.ConnectionString = strConnect;
            cmd.Connection = cnn;
        }
        public void Connect()
        {
            cnn.Open();
        }

        public void Disconnect()
        {
            cnn.Close();
        }

        public DataTable ExecuteQuery(string strSQL)
        {
            Connect();

            cmd.CommandText = strSQL;
            SqlDataReader sdr = cmd.ExecuteReader();
            DataTable dt = new DataTable();

            dt.Load(sdr);
            sdr.Close();
            Disconnect();
            return dt;
        }

        public bool ExecuteNonQuery(string strSQL)
        {
            Connect();
            try
            {
                cmd.CommandText = strSQL;
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                Disconnect();
                return false;
            }
            Disconnect();
            return true;
        }
    }
}